package com.chery.media.music

import android.content.ComponentName
import android.content.Context
import android.media.browse.MediaBrowser
import android.media.session.MediaController
import com.blankj.utilcode.util.LogUtils


class BluetoothMusicHelp(val context: Context) {

    var mMediaBrowser: MediaBrowser? = null

    var mMediaController: MediaController? = null


    var mTryConnectMediaSessionCount = 0

    /**
     * 连接MediaBrowser
     */
    fun connectMediaBrowser() {
        if (mMediaBrowser != null) {
            disconnectMediaBrowser()
        }
        LogUtils.eTag("BluetoothMusicHelp", "开始连接MediaBrowser")
        mMediaBrowser = MediaBrowser(
            context,
            ComponentName(
                "com.android.bluetooth",
                "com.android.bluetooth.avrcpcontroller.BluetoothMediaBrowserService"
            ),
            mConnectionCallback,
            null
        )
        mMediaBrowser?.connect()
    }

    /**
     * 断开连接MediaBrowser
     */
    fun disconnectMediaBrowser() {
        LogUtils.eTag("BluetoothMusicHelp", "断开连接MediaBrowser")
        if (mMediaBrowser != null) {
            mMediaBrowser?.disconnect()
            mMediaBrowser = null
        }
    }

    val mConnectionCallback = object : MediaBrowser.ConnectionCallback() {
        override fun onConnected() {
            if (mMediaBrowser?.sessionToken != null) {
                LogUtils.eTag("BluetoothMusicHelp", "连接MediaBrowser成功")
                mMediaController = MediaController(context, mMediaBrowser?.sessionToken!!)
                mTryConnectMediaSessionCount = 0
            } else {
                throw IllegalArgumentException("No Session token")
            }
        }

        override fun onConnectionFailed() {
            LogUtils.eTag("BluetoothMusicHelp", "连接MediaBrowser失败")
            if (mTryConnectMediaSessionCount <= 3) {
                connectMediaBrowser()
            }
        }

        override fun onConnectionSuspended() {
            LogUtils.eTag("BluetoothMusicHelp", "连接MediaBrowser暂停")
            mTryConnectMediaSessionCount = 0
        }

    }

    fun operateMusic(type: OperateMusicType): Boolean {
        if (mMediaController == null || mMediaController?.transportControls == null) {
            return false
        }
        val mediaControllerCntrl = mMediaController?.transportControls
        when(type) {
            OperateMusicType.PLAY -> {
                mediaControllerCntrl?.play()
            }
            OperateMusicType.PAUSE -> {
                mediaControllerCntrl?.pause()
            }
            OperateMusicType.NEXT -> {
                mediaControllerCntrl?.skipToNext()
            }
            OperateMusicType.PREVIOUS -> {
                mediaControllerCntrl?.skipToPrevious()
            }
            OperateMusicType.STOP -> {
                mediaControllerCntrl?.stop()
            }
        }
        return true
    }


}

enum class OperateMusicType {

    PLAY, // 播放

    PAUSE, // 暂停

    NEXT, //下一首

    PREVIOUS, // 上一首

    STOP, // 停止

}